<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <el-button style="float:right" type="primary" @click="handleSearchList()" size="mini">
          查询搜索
        </el-button>
        <el-button style="float:right;margin-right: 15px" @click="handleResetSearch()" size="mini">
          重置
        </el-button>
      </div>
      <div>
        <el-form :inline="true" :model="listQuery" size="mini" label-width="100px">
          <el-form-item label="标题：">
            <el-input v-model="listQuery.title" class="input-width" placeholder="请输入标题" clearable></el-input>
          </el-form-item>
          <el-form-item label="类型：">
              <el-select v-model="listQuery.type" placeholder="请选择类型">
                <el-option
                  v-for="item in typeOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>通知列表</span>
      <el-button size="mini" type="success" class="btn-add" @click="handleAdd" style="margin-left: 20px">添加通知</el-button>
    </el-card>
    <div class="table-container message-container">
      <el-table ref="adminTable" size="mini" :data="list" style="width: 100%;" v-loading="listLoading" border>
         <el-table-column label="ID" width="120" align="center">
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="标题"  align="center">
          <template slot-scope="scope">{{ scope.row.title }}</template>
        </el-table-column>
         <el-table-column label="正文内容"  align="center">
          <template slot-scope="scope">{{ scope.row.content }}</template>
        </el-table-column>
         <el-table-column label="消息类型" width="100"   align="center">
          <template slot-scope="scope">{{ messageType[scope.row.type] }}</template>
        </el-table-column>
        <el-table-column label="添加时间" width="180" align="center">
          <template slot-scope="scope">{{ scope.row.createTime | formatDateTime }}</template>
        </el-table-column>
        <el-table-column label="操作" width="130" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="handleUpdata(scope.row)"
              >编辑</el-button>
            <el-button size="mini" type="text" @click="handleDelete(scope.row)"
             >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper" :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize" :page-sizes="[5, 10, 15, 20]" :total="total">
      </el-pagination>
    </div>

    <!-- 新增/编辑  -->
    <edit-message-dialog  v-if="editVisible" :row="row"  @handle-confirm="handleConfirm" @handle-cancel="editVisible=false" />

  </div>
</template>
<script>
import { getMessageList,deleteMessage } from '@/api/message/index';
import { formatDate } from '@/utils/date';
import EditMessageDialog from "./dialogs/edit-message-dialog.vue";
import { createNamespacedHelpers } from "vuex";
const { mapState } = createNamespacedHelpers("message");
const defaultListQuery = {
  pageNum: 1,
  pageSize: 10,
  title: null,
  type:''
};
export default {
  name: 'messageList',
  components: {
    EditMessageDialog
  },
   computed: {
    ...mapState({
      messageType: state => state.routes.messageType
    })
  },
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      total: null,
      row:{},
      listLoading: false,
      editVisible: false,
      typeOptions:[{label:'全部',value:''}]
    }
  },
  created() {
    let typeList=Object.keys(this.messageType).map(key=>{
      return{
        label:this.messageType[key],
        value:key
      }
    })
    this.typeOptions=[...this.typeOptions,...typeList]
    this.getList();
  },
  filters: {
    formatDateTime(time) {
      if (time == null || time === '') {
        return 'N/A';
      }
      let date = new Date(time);
      return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
    }
  },
  methods: {
    handleResetSearch() {
      this.listQuery = Object.assign({}, defaultListQuery);
      this.getList();
    },
    handleSearchList() {
      this.listQuery.pageNum = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },
  
    handleConfirm() {
      this.getList();
      this.editVisible = false;
    },

    // 添加
    handleAdd() {
      this.row={};
      this.editVisible = true;
    },
    // 编辑
    handleUpdata(row){
       this.row=row;
       this.editVisible = true;
    },
 
    //删除
    handleDelete(row) {
      this.$confirm('是否要删除消息?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteMessage({id:row.id}).then(response => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.getList();
        });
      });
    },
   
    getList() {
      this.listLoading = true;
      getMessageList(this.listQuery).then(response => {
        this.listLoading = false;
        this.list = response.data.list;
        this.total = response.data.total;
      });
    }

   
  }
}
</script>
<style lang="scss">
.success{
  color: #67c23a!important;
}
.danger{
  color: #f56c6c!important;
}
.message-container{
 height: calc(100vh - 242px);
  .el-table__body-wrapper{
    height: calc(100vh - 295px);
    overflow: auto;
  }
}
</style>